<template>
  <div class="home-page">
    <!-- 展示组件 -->
    <div class="content-section">
      <!-- 网站标题区域 -->
      <div class="header-section">
        <div class="title-container">
          <h1 class="main-title">AI 应用生成平台</h1>
        </div>
        <p class="subtitle">一句话轻松创建网页应用</p>
      </div>

      <!-- 用户提示词输入框 -->
      <div class="prompt-section">
        <div class="prompt-container">
          <div class="prompt-content">
            <!--输入框-->
            <a-textarea
              v-model:value="promptText"
              placeholder="描述越详细,页面越具体,可以一步一步完善生成效果"
              :rows="4"
              class="prompt-input"
              @keydown.ctrl.enter="handleCreateApp"
            />
            <!--按钮-->
            <div class="prompt-actions">
              <div class="action-left">
                <a-button type="text" class="action-btn">
                  <template #icon>
                    <PaperClipOutlined />
                  </template>
                  上传
                </a-button>
                <a-button type="text" class="action-btn">
                  <template #icon>
                    <HighlightOutlined />
                  </template>
                  优化
                </a-button>
              </div>
              <a-button type="primary" shape="circle" class="submit-btn" @click="handleCreateApp">
                <template #icon>
                  <UpOutlined />
                </template>
              </a-button>
            </div>
          </div>
        </div>
      </div>

      <!-- 快速标签 -->
      <div class="tags-section">
        <a-button v-for="tag in quickTags" :key="tag" class="tag-btn" @click="handleTagClick(tag)">
          {{ tag }}
        </a-button>
      </div>
    </div>

    <!-- 精选案例 -->

    <div class="featured-section">
      <a-card class="featured-card">
        <div class="section-header">
          <h2 class="section-title">精选案例</h2>
        </div>
        <!--主体-->
        <div class="app-grid">
          <AppCard
            v-for="app in featuredApps"
            :key="app.id"
            :app="app"
            :featured="true"
            @view-chat="viewChat"
            @view-work="viewWork"
          />
        </div>

        <div v-if="featuredApps.length === 0" class="empty-state">
          <div class="empty-content">
            <StarOutlined class="empty-icon" />
            <p>暂无精选应用</p>
          </div>
        </div>
        <div v-else class="pagination-wrapper">
          <a-pagination
            v-model:current="featuredAppsQuery.current"
            v-model:pageSize="featuredAppsQuery.pageSize"
            :total="featuredAppsTotal"
            :show-size-changer="false"
            @change="loadFeaturedApps"
          />
        </div>
      </a-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import {
  HighlightOutlined,
  PaperClipOutlined,
  StarOutlined,
  UpOutlined,
} from '@ant-design/icons-vue'
import { addApp, listGoodAppVoByPage } from '@/api/appController'
import AppCard from '@/components/AppCard.vue'
import { getDeployUrl } from '@/config/env'

const router = useRouter()

// 响应式数据
const promptText = ref('')
const featuredApps = ref<API.AppVO[]>([])
const featuredAppsTotal = ref(0)

const featuredAppsQuery = ref<API.AppQueryRequest>({
  current: 1,
  pageSize: 6,
})

// 快速标签
const quickTags = ['波普风电商页面', '企业网站', '电商运营后台', '暗黑话题社区']

// 方法
const handleCreateApp = async () => {
  if (!promptText.value.trim()) {
    message.warning('请输入应用描述')
    return
  }

  try {
    const res = await addApp({
      initPrompt: promptText.value,
    })
    if (res.code === 0 && res.data) {
      message.success('应用创建成功')
      router.push(`/app/chat/${res.data}`)
    } else {
      message.error(res.message || '创建失败')
    }
  } catch (error) {
    message.error('创建失败')
    console.error(error)
  }
}

const handleTagClick = (tag: string) => {
  promptText.value = tag
}

const loadFeaturedApps = async () => {
  try {
    const res = await listGoodAppVoByPage(featuredAppsQuery.value)
    if (res.code === 0 && res.data) {
      featuredApps.value = res.data.records || []
      featuredAppsTotal.value = Number(res.data.total) || 0
    }
  } catch (error) {
    console.error('加载精选应用失败:', error)
  }
}

// 查看对话
const viewChat = (appId: string | number | undefined) => {
  if (appId) {
    router.push(`/app/chat/${appId}`)
  }
}

// 查看作品
const viewWork = (app: API.AppVO) => {
  if (app.deployKey) {
    const url = getDeployUrl(app.deployKey)
    window.open(url, '_blank')
  }
}

// 生命周期
onMounted(() => {
  loadFeaturedApps()
})
</script>

<style scoped>
.home-page {
  //min-height: 100vh;
  //background: linear-gradient(180deg, #f0f8ff 0%, #ffffff 100%);
  //padding: 40px 20px;

  width: 100%;
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, #f8fafc 0%, #f1f5f9 8%, #e2e8f0 20%, #cbd5e1 100%),
    radial-gradient(circle at 20% 80%, rgba(59, 130, 246, 0.15) 0%, transparent 50%),
    radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.12) 0%, transparent 50%),
    radial-gradient(circle at 40% 40%, rgba(16, 185, 129, 0.08) 0%, transparent 50%);
  position: relative;
  overflow: hidden;
}

/* 科技感网格背景 */
.home-page::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    linear-gradient(rgba(59, 130, 246, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59, 130, 246, 0.05) 1px, transparent 1px),
    linear-gradient(rgba(139, 92, 246, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(139, 92, 246, 0.04) 1px, transparent 1px);
  background-size:
    100px 100px,
    100px 100px,
    20px 20px,
    20px 20px;
  pointer-events: none;
  animation: gridFloat 20s ease-in-out infinite;
}

/* 动态光效 */
.home-page::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(
      600px circle at var(--mouse-x, 50%) var(--mouse-y, 50%),
      rgba(59, 130, 246, 0.08) 0%,
      rgba(139, 92, 246, 0.06) 40%,
      transparent 80%
    ),
    linear-gradient(45deg, transparent 30%, rgba(59, 130, 246, 0.04) 50%, transparent 70%),
    linear-gradient(-45deg, transparent 30%, rgba(139, 92, 246, 0.04) 50%, transparent 70%);
  pointer-events: none;
  animation: lightPulse 8s ease-in-out infinite alternate;
}

@keyframes gridFloat {
  0%,
  100% {
    transform: translate(0, 0);
  }
  50% {
    transform: translate(5px, 5px);
  }
}

@keyframes lightPulse {
  0% {
    opacity: 0.3;
  }
  100% {
    opacity: 0.7;
  }
}

.content-section {
  min-height: 70vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.header-section {
  text-align: center;
  margin-bottom: 60px;
}

.title-container {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 16px;
}

.title-icon {
  font-size: 24px;
  margin-right: 12px;
}

.main-title {
  font-size: 48px;
  font-weight: bold;
  color: #000;
  margin: 0;
}

.subtitle {
  font-size: 18px;
  color: #666;
  margin: 0;
}

.prompt-section {
  min-width: 800px;
  margin: 0 auto;
}

.prompt-container {
  background: white;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: 1px solid #e8f4fd;
}

.prompt-content {
  //margin-bottom: 20px;
}

.prompt-text {
  font-size: 16px;
  line-height: 1.6;
  color: #333;
  margin-bottom: 16px;
  min-height: 80px;
}

.prompt-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.action-left {
  display: flex;
  gap: 16px;
}

.action-btn {
  display: flex;
  align-items: center;
  color: #666;
}

.submit-btn {
  background: #000;
  border-color: #000;
}

.submit-btn:hover {
  background: #333;
  border-color: #333;
}

.prompt-input {
  border: none;
  resize: none;
  font-size: 16px;
  line-height: 1.6;
}

.prompt-input:focus {
  box-shadow: none;
  border: none;
}

.tags-section {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin: 30px;
  flex-wrap: wrap;
}

.tag-btn {
  border-radius: 20px;
  background: #e8f4fd;
  border: none;
  color: #333;
  padding: 8px 16px;
  height: auto;
}

.tag-btn:hover {
  background: #d1e7fd;
  color: #000;
}

.works-section,
.featured-section {
  max-width: 1200px;
  margin: 0 auto 60px;
}

.featured-card {
  border-radius: 32px;
  padding: 16px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 32px;
  flex-wrap: wrap;
  gap: 16px;
}

.section-title {
  font-size: 32px;
  font-weight: bold;
  color: #000;
  margin: 0;
}

.search-box {
  width: 300px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.empty-icon {
  font-size: 48px;
  color: #ccc;
}

.empty-content p {
  font-size: 16px;
  margin: 0;
}

.app-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 32px;
}

.app-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition:
    transform 0.2s,
    box-shadow 0.2s;
}

.app-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.app-preview {
  height: 200px;
  background: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.preview-content {
  text-align: center;
}

.preview-content h3 {
  margin: 0 0 16px 0;
  font-size: 18px;
  color: #333;
}

.preview-bars {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.bar {
  height: 8px;
  background: #ddd;
  border-radius: 4px;
  width: 100%;
}

.preview-desc {
  color: #666;
  margin: 8px 0;
  font-size: 14px;
}

.preview-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}

.app-info {
  padding: 16px;
}

.app-title {
  margin: 0 0 8px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.app-time,
.app-author {
  margin: 0;
  font-size: 14px;
  color: #999;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 32px;
}

@media (max-width: 768px) {
  .main-title {
    font-size: 32px;
  }

  .app-grid {
    grid-template-columns: 1fr;
  }

  .prompt-container {
    margin: 0 16px;
  }

  .section-header {
    flex-direction: column;
    align-items: stretch;
  }

  .search-box {
    width: 100%;
  }

  .section-title {
    text-align: center;
  }
}
</style>
